package com.sczmgk.frame.http;


import com.sczmgk.hotline.utils.MyLog;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpHelper {
	
	/**
	 * TAG
	 */
	private static final String TAG = HttpHelper.class.getSimpleName();
	
	public static String initURL(String url, Object data){
		try {
			if (!hasProtocol(url)) {
				return null;
			}
			if(data!=null){
				String paramData = getRequestURLParam(data);
				return url +"?"+ paramData;
			}else{
				return url;
			}
			
		} catch (UnsupportedEncodingException e) {
			MyLog.e(TAG, "httpFrame threadName:"+ Thread.currentThread().getName()+" initURL HttpHelper initURL  瀛樺湪闈炴硶鐨勪紶鍙� "+data);
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public static String getRequestURLParam(Object object) throws UnsupportedEncodingException {
		if(object == null){
			return "";
		}
		String requestData = "";
		if (object instanceof Map) {
			Map<String, String> paramMap = (Map<String, String>) object;
			requestData = getRequestURLParam(paramMap);
		} else {
			requestData = (String) object;
		}
		return requestData;
	}

	private static String getRequestURLParam(Map<String, String> paramMap
			) throws UnsupportedEncodingException {

		StringBuffer queryBuffer = new StringBuffer();

		if (paramMap == null) {
			return queryBuffer.toString();
		}

		Iterator<String> it = paramMap.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			Object obj = paramMap.get(key);
			String valuse = "";
			if(obj instanceof byte[]){
				valuse = new String((byte[])obj);
			}else{
				valuse = ""+obj;
			}

			if (!"".equals(queryBuffer.toString())) {
				queryBuffer.append("&");
			}
			queryBuffer.append(key);
			queryBuffer.append("=");
			queryBuffer.append(valuse);
		}
		return queryBuffer.toString();

	}
	
	/**
	 * ssl not verify
	 */
	public static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	};

	/**
	 * [Trust every server - dont check for any certificate]<BR>
	 */
	public static void trustAllHosts() {
		// Create a trust manager that does not validate certificate chains
		TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
			public X509Certificate[] getAcceptedIssuers() {
				return new X509Certificate[]{};
			}

			public void checkClientTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {
			}

			public void checkServerTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {
			}
		}};

		// Install the all-trusting trust manager
		try {
			SSLContext sc = SSLContext.getInstance("TLS");
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
			HttpsURLConnection
					.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String getHostFromUrl(String urlString) {
		String host = null;
		try {
			URL url = new URL(urlString);
			host = url.getProtocol()+"://"+url.getHost();
		} catch (MalformedURLException e) {
			e.printStackTrace();
			MyLog.e(TAG, "httpFrame  threadName:"+ Thread.currentThread().getName()+" getHostFromUrl "+e.getMessage());
		}
		
		return host;
	}
	public static boolean hasProtocol(String sendURL) {
		boolean hasProtocol = false;
		try {
			URL url = new URL(sendURL);
			url.getProtocol();
			hasProtocol = true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			MyLog.e(TAG, "httpFrame  threadName:"+ Thread.currentThread().getName()+" hasProtocol "+e.getMessage());
		}
		return hasProtocol;
	}

	
}
